Lumen Programming Guide by Paul Redmond

Lumen Programming Guide by Paul Redmond

Author:Paul Redmond
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


The constructor method type-hints the FractalResponse argument that the service container will resolve automatically when initializing the controller. The item and collection methods are pass-through methods to the FractalResponse service. You could make the methods more generic like respondWithItem and take care of the response, but what you have is good enough for now. You can refactor when you sense duplication and brittle code.

Let’s try your first integration with the BooksController. Be sure to add use App\Transformer\BookTransformer; and extends Controller to start using the base application controller at the top of app/Http/Controllers/BooksController.php (Listing 7-56).

1 <?php

2

3 namespace App\Http\Controllers;

4

5 use App\Book;

6 use Illuminate\Http\Request;

7 use App\Transformer\BookTransformer;

8 use Illuminate\Database\Eloquent\ModelNotFoundException;

9

10 /**

11 * Class BooksController

12 * @package App\Http\Controllers

13 */

14 class BooksController extends Controller

15 {

16 /**

17 * GET /books

18 * @return array

19 */

20 public function index()

21 {

22 return $this->collection(Book::all(), new BookTransformer());

23 }

24 // ...

25 }

Listing 7-56.Using the FractalResponse Service in the BooksController



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.